/*
 * baseaicode13
 */
package com.kun.sun.baseaicode13.service;

import com.kun.sun.core.entity.Page;
import com.kun.sun.core.exceptions.BaseException;
import com.kun.sun.core.exceptions.Baseaicode13Exception;
import com.kun.sun.baseaicode13.dao.Baseaicode13DAO;
import com.kun.sun.baseaicode13.entity.Baseaicode13State;
import com.kun.sun.baseaicode13.entity.Baseaicode13;
import com.kun.sun.core.base.BaseDAO;
import com.kun.sun.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

/**
* 
*
* @author baseaicode13
*/
@Service("baseaicode13SV")
@Slf4j
public class Baseaicode13SVImpl extends BaseSVImpl<Baseaicode13, Long> implements Baseaicode13SV {

    @Autowired
    private Baseaicode13DAO baseaicode13DAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return baseaicode13DAO;
    }

    /**
    * 保存baseaicode13对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(Baseaicode13 entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));
        super.save(entity);
    }

    /**
     * 加载一个对象Baseaicode13
     * @param id * @param code 
     * @return Baseaicode13
     */
    @Override
    public Baseaicode13 load(java.lang.Long id,java.lang.Object code) {
           if(id==null&&code==null){
                 throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return baseaicode13DAO.load(param);
    }
    /**
     * 加载一个对象Baseaicode13 通过id
     * @param id 
     * @return Baseaicode13
     */
    @Override
    public Baseaicode13 loadById(java.lang.Long id) {
            if(id==null){
                 throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return baseaicode13DAO.loadById(id);
    }

    /**
     * 加载一个对象Baseaicode13 通过code
     * @param code 
     * @return Baseaicode13
     */
    @Override
    public Baseaicode13 loadByCode(java.lang.Object code) {
            if(code==null){
                 throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return baseaicode13DAO.loadByCode(code);
    }


    /**
    * 根据主键id,oldStates 共同更新 Baseaicode13 的状态到newState状态
    *
    * @param id 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateById(java.lang.Long id,Baseaicode13State newState,Baseaicode13State... oldStates){
        if(id==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        baseaicode13DAO.updateStateById(id,new Date(),newState,oldStates);
    }
    /**
    * 根据主键code,oldStates 共同更新 Baseaicode13 的状态到newState状态
    *
    * @param code 
    * @param newState 新状态
    * @param oldStates 旧状态集合
    */
    @Override
    public void updateStateByCode(java.lang.Object code,Baseaicode13State newState,Baseaicode13State... oldStates){
        if(code==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(newState==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if(oldStates==null){
            throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        baseaicode13DAO.updateStateByCode(code,new Date(),newState,oldStates);
    }

    /**
    * 根据主键id 更新 Baseaicode13 的状态到另一个状态
    *
    * @param id 
    * @param state 状态
    */
    @Override
    public void updateById(java.lang.Long id,Baseaicode13State state){
            if(id==null){
               throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          baseaicode13DAO.updateById(id,state,new Date());
    }
    /**
    * 根据主键code 更新 Baseaicode13 的状态到另一个状态
    *
    * @param code 
    * @param state 状态
    */
    @Override
    public void updateByCode(java.lang.Object code,Baseaicode13State state){
            if(code==null){
               throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
          baseaicode13DAO.updateByCode(code,state,new Date());
    }


    /**
     * 删除对象Baseaicode13
     * @param id * @param code 
     * @return Baseaicode13
     */
    @Override
    public void delete(java.lang.Long id,java.lang.Object code) {
            if(id==null&&code==null){
                throw new Baseaicode13Exception(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            baseaicode13DAO.delete(param);
    }



    /**
    * 查询Baseaicode13分页
    *
    * @param baseaicode13  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<Baseaicode13>
    */
    @Override
    public List<Baseaicode13> list(Baseaicode13 baseaicode13, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (baseaicode13 != null) {
               map = JSON.parseObject(JSON.toJSONString(baseaicode13, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return baseaicode13DAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(Baseaicode13 baseaicode13){
        Map<String, Object> map = null;
        if (baseaicode13 != null) {
           map = JSON.parseObject(JSON.toJSONString(baseaicode13, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return baseaicode13DAO.count(map);
     }

    /**
     * 查询Baseaicode13分页
     *
     * @param id  * @param code  
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Baseaicode13>
     */
    @Override
    public List<Baseaicode13> list(java.lang.Long id,java.lang.Object code,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return baseaicode13DAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id,java.lang.Object code) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            param.put("code",code);
            return baseaicode13DAO.count(param);
    }

}
